<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>광장</h1>
<script>
function addmessage()
{
  new Ajax.Request('../post/postChattingAddMsg.jsp',
  {
     method: 'post',
     parameters: $('form').serialize(),
     onSuccess: function( transport ) {
       $('msgType').value = '';
     }
  } );
}
</script>
<form name="form">
            <td width="78"><select name="chatCD" size="1">
                    <option value="CT001">방채팅</option>
                    <option value="CT002">귓속말</option>
                    <option value="CT003">길드채팅</option>
</select></td>
            <td width="147">
                <p><span style="font-size:9pt;"><input type="text" name="toWho" maxlength="20" size="20"></span></p>
            </td>
            <td width="482">
                <p><span style="font-size:9pt;"><input type="text" name="chatType" id="msgType" size="75"></span></p>
            </td>
            <td width="10">
                <p><span style="font-size:9pt;"><button onclick="addmessage()">Add</button></span></p>
            </td>
        </tr>
    </table>
</form>

<script>
var lastid = 0;
function getMessages()
{
  new Ajax.Request( '../post/postChattingMessage.jsp?lastid='+lastid, {
	  onSuccess: function( transport ) {
      var messages = transport.responseXML.getElementsByTagName( 'message' );
      for( var i = 0; i < messages.length; i++ )
      {
		var message = messages[i].firstChild.nodeValue;
		var id = parseInt(messages[i].getAttribute('id'));
		var serverNo = messages[i].getAttribute('serverNo');
		var senderCharNo = messages[i].getAttribute('senderCharNo');
		var senderName = messages[i].getAttribute('senderName');
		var chatCdClassify = messages[i].getAttribute("chatCdClassify");
		var chatCD = messages[i].getAttribute('chatCD');
		if(chatCD == "CT001") {
			chatCD = "방채팅";
		} else if(chatCD == "CT002") {
			chatCD = "귓속말";
		} else {
			chatCD = "길드채팅";
		}
        if ( id > lastid )
        {
          var elTR = $('chat').insertRow( -1 );
          var elTD1 = elTR.insertCell( -1 );
          elTD1.appendChild( document.createTextNode( "[" + senderName + "]") );
          var elTD2 = elTR.insertCell( -1 );
          elTD2.appendChild( document.createTextNode( "[" + chatCD + "] : " ) );
          var elTD3 = elTR.insertCell( -1 );
          elTD2.appendChild( document.createTextNode( message ) );

          lastid = id;
        }
      }
      window.setTimeout( getMessages, 1000 );
    }
  } );
}
getMessages();
</script>

</body>
</html>